<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>闭包陷阱</title>
</head>
<body>
    <button id="btn">Add</button>
    <script>
        // 闭包导致内存泄漏  
        // function foo (){
        //     var el = document.getElementById('btn')
        //     el.onclick = function(){
        //         console.log(el.id)
        //     }
        // }
        // foo()

        // 闭包陷阱解决办法  
        function foo (){
            var el = document.getElementById('btn')
            el.onclick = function(){
                console.log(el.id)
            }
            el= null
        }
        foo()
    </script>
</body>
</html>